package com.woniuxy.personal.core.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * Auther: binhu  <br/>
 * Date: 2021/6/30:14:22  <br/>
 * Description:
 */
@Configuration
public class RabbitMQConfig {

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){
        RabbitTemplate rabbitTemplate = new RabbitTemplate();
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setConnectionFactory(connectionFactory);
        return rabbitTemplate;
    }



    @Bean
    public TopicExchange orderExchange(){
        return new TopicExchange("order-exchange",true,false);
    }

    @Bean
    public Queue orderQueue(){
        return new Queue("order-queue",true,false,false);
    }

    @Bean
    public Binding bindOrderQueueExchange(){
        return BindingBuilder.bind(orderQueue()).to(orderExchange()).with("order.#");
    }


    @Bean
    public TopicExchange ttlExchange(){
        return new TopicExchange("ttl-exchange",true,false);
    }

    @Bean
    public Queue ttlQueue(){
        Map map = new HashMap();
        map.put("x-message-ttl",60000L);
        map.put("x-dead-letter-exchange","dlq-exchange");
        map.put("x-dead-letter-routing-key","dead.order");
        return new Queue("ttl-queue",true,false,false,map);
    }

    @Bean
    public Binding bindTtlQueueExchange(){
        return BindingBuilder.bind(ttlQueue()).to(ttlExchange()).with("ttl.#");
    }

    @Bean
    public TopicExchange dlqExchange(){
        return new TopicExchange("dlq-exchange",true,false);
    }

    @Bean
    public Queue dlqQueue(){
        return new Queue("dlq-queue",true,false,false);
    }

    @Bean
    public Binding bindDlqQueueExchange(){
        return BindingBuilder.bind(dlqQueue()).to(dlqExchange()).with("dead.order.#");
    }

}
